<template>
  <div class="container">
    <div class="login-box">
      <div class="login-title">管理员登录</div>
      <div>
        <div class="login-item">
          <el-tag type="warning">账号:</el-tag>
          <el-input
            id="username"
            name="username"
            placeholder="请输入账号"
            v-model="state.account"
            @keydown.enter="($refs.password as HTMLElement).focus()"
          />
        </div>
        <div class="login-item">
          <el-tag type="warning">密码:</el-tag>
          <el-input
            ref="password"
            id="password"
            name="password"
            type="password"
            placeholder="请输入密码"
            v-model="state.password"
            @keydown.enter="login"
          />
        </div>
        <div class="login-item">
          <el-button type="primary" @click="login" class="login-btn"
            >登录</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive } from 'vue';
import { useRouter } from 'vue-router';
import { toast } from '../../utils/tools';
import { throttle } from '../../utils/tools';
import { login as useLogin } from '../../functions/login';
import { setToken } from '../../functions/token';
import loading from '../../utils/loading';
const state = reactive({
  account: 'zhangsan',
  password: '123456',
});
const router = useRouter();
const login = throttle(async () => {
  var res = await useLogin(state.account, state.password);
  if (res.code >= 0) {
    toast.success(res.message);
    setToken(res.data.token);
    // 跳转到首页
    loading.show('正在进入首页...');
    router.push('/');
  } else {
    toast.error(res.message);
  }
});
</script>

<style scoped lang="scss">
.container {
  background-color: aquamarine;
  height: 100vh;
}
.login-box {
  background-color: white;
  width: 300px;
  position: relative;
  top: 100px;
  margin: auto;
  border-radius: 10px;
  padding: 20px;
}
.login-title {
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 20px;
}
.login-item {
  margin-bottom: 10px;
}
.login-btn {
  width: 100%;
  display: block;
}
</style>
